/**
 * 
 */
Ext.ns("conductionApply");
conductionApply.init = function() {
	conductionApply.isRight = false;// 表单填写是否正确
	conductionApply.isEdit = false;// 是否进行修改
	conductionApply.big_Id;// 定义选定的大类型的序列号
	conductionApply.big_Name;// 定义选定的大类型名称
	conductionApply.small_Id;// 定义选定的小类型的序列号
	conductionApply.small_Name;// 定义选定的大类型名称
	conductionApply.l_id;
	conductionApply.l_time;
	conductionApply.l_etime;
	conductionApply.exam_Id;
	conductionApply.exam_Name;
	conductionApply.startTime;
	conductionApply.endTime;
	conductionApply.isRight = false;// 定义是否已经退订过考场
	conductionApply.newN = true;// 定义是否点击的为新考试评判选项状态栏
	conductionApply.selected;
	conductionApply.applyCount = 0;// 需要分配的考生个数
	conductionApply.personCount = 0;// 单向考试报考总人数
	conductionApply.includeCount = 0;// 所选考点可容考生的总数
	conductionApply.sumPersonCount = 0;// 一共需要分配多少考生
	conductionApply.sumPersonCountR = 0;// 备份一共需要分配多少考生
	conductionApply.menuClick;
	conductionApply.resultForSearch;
	conductionApply.personCountForSearch;
	conductionApply.manualArrage = false;
	conductionApply.autoApply = false;// 确定是否已经自动安排考点，只有在自动安排考点之后，才能对考场自动安排经行安排。
	conductionApply.autoApplyPosition = false;// 确定是否已经使用了自动考点考场安排，如果已经安排，就不可以手动安排，除非消除重新分配
	conductionApply.sm = new Ext.grid.CheckboxSelectionModel({
				handleMouseDown : Ext.emptyFn
			});
	conductionApply.cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			conductionApply.sm, {
				header : '申办学校或单位',
				dataIndex : 'name',
				sortable : true
			}, {
				header : '地区',
				dataIndex : 'city',
				sortable : true
			}, {
				header : '区域',
				dataIndex : 'region',
				sortable : true
			}, {
				header : '报考人数',
				dataIndex : 'personCount',
				sortable : true
			}, {
				header : '可容纳人数',
				dataIndex : 'includeCount',
				sortable : true
			}]);
	conductionApply.lcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			{
				header : '考试名称',
				dataIndex : 'l_name',
				sortable : true,
				width : 30
			}, {
				header : '考试时间',
				dataIndex : 'l_time',
				renderer : Ext.util.Format.dateRenderer('Y年m月d日   h时i分'),
				sortable : true,
				width : 20
			}, {
				header : '申办个数',
				dataIndex : 'conduction_Count',
				sortable : true,
				width : 15
			}, {
				header : '考生人数',
				dataIndex : 'l_person_Count',
				sortable : true,
				width : 15
			}]);
	conductionApply.lstore = new Ext.data.JsonStore({
				url : 'center/showExamListForCenter.action',
				totalProperty : 'total',
				root : 'examValues',
				fields : [{
							name : "l_id",
							mapping : 'l_id',
							type : 'int'
						}, {
							name : "l_name",
							mapping : 'l_name',
							type : 'string'
						}, {
							name : "l_time",
							mapping : 'l_time',
							type : 'string'
						}, {
							name : "l_etime",
							mapping : 'l_etime',
							type : 'string'
						}, {
							name : "conduction_Count",
							mapping : 'conduction_Count',
							type : 'int'
						}, {
							name : "l_person_Count",
							mapping : 'l_person_Count',
							type : 'int'
						}]
			});
	conductionApply.store = new Ext.data.JsonStore({
				url : 'center/showConductionListForCenter.action',
				totalProperty : 'total',
				root : 'conductions',
				fields : [{
							name : "id",
							mapping : 'id',
							type : 'int'
						}, {
							name : "no",
							mapping : 'no',
							type : 'int'
						}, {
							name : "name",
							mapping : 'name',
							type : 'string'
						}, {
							name : "city",
							mapping : 'city',
							type : 'string'
						}, {
							name : "region",
							mapping : 'region',
							type : 'string'
						}, {
							name : "personCount",
							mapping : 'personCount',
							type : 'int'
						}, {
							name : "includeCount",
							mapping : 'includeCount',
							type : 'int'
						}]
			});

	conductionApply.BigTypes = new Ext.data.JsonStore({
				url : 'public/showBigList.action',
				totalProperty : 'total',
				root : 'bigtypes',
				fields : [{
							name : "big_Id",
							type : 'int'
						}, {
							name : "big_Name",
							type : 'string'
						}]
			});
	conductionApply.SmallTypes = new Ext.data.JsonStore({
				url : 'public/showSmallList.action',
				totalProperty : 'total',
				root : 'smalltypes',
				baseParams : {
					start : 0
				},
				fields : [{
							name : "small_Id",
							mapping : 'small_Id',
							type : 'int'
						}, {
							name : "small_Name",
							mapping : 'small_Name',
							type : 'string'
						}]
			});
	conductionApply.LPanel = new Ext.grid.GridPanel({
				columnWidth : .35,
				cm : conductionApply.lcm,
				store : conductionApply.lstore,
				frame : true,
				loadMask : true,
				autoScroll : true,
				height : 480,
				viewConfig : {
					columnsText : '选择显示列',
					forceFit : true
				},
				tbar : new Ext.Toolbar(['-', {
							id : 'conductionApply_search_bigType',
							fieldLabel : '考试类型',
							allowBlank : false,
							emptyText : '请选择类型名查询',
							blankText : '对不起，请选择考试类型',
							xtype : 'combo',
							mode : 'remote',
							triggerAction : 'all',
							valueField : 'big_Id',
							displayField : 'big_Name',
							pageSize : 8,
							width : 120,
							minListWidth : 300,
							store : conductionApply.BigTypes
						}, '-', {
							id : 'conductionApply_search_smallType',
							fieldLabel : '考试类型细节',
							allowBlank : false,
							emptyText : '请选择类型细节',
							blankText : '对不起，请选择类型细节进行查询',
							name : 'examValue.exam_Type',
							xtype : 'combo',
							mode : 'local',
							triggerAction : 'all',
							valueField : 'small_Id',
							displayField : 'small_Name',
							pageSize : 8,
							width : 120,
							minListWidth : 300,
							store : conductionApply.SmallTypes
						}, '-', {
							text : '新一周',
							handler : function() {
								conductionApply.lstore.load({
											params : {
												state : 1
											}
										});
							}
						}, '-', {
							text : '新一月',
							handler : function() {
								conductionApply.lstore.load({
											params : {
												state : 2
											}
										});
							}
						}])
			});
	conductionApply.RPanel = new Ext.grid.GridPanel({
		columnWidth : .65,
		sm : conductionApply.sm,
		cm : conductionApply.cm,
		frame : true,
		store : conductionApply.store,
		loadMask : true,
		height : 480,
		autoScroll : true,
		viewConfig : {
			columnsText : '选择显示列',
			forceFit : true
		},
		tbar : {
			xtype : 'toolbar',
			autoWidth : true,
			forceLayout : true,
			items : ['-', {
						text : '图形分析',
						handler : function() {
							chart.conductionApplyChartGraph.show();
						}
					}, '-', {
						text : '确定考点',
						handler : function() {
							if (!conductionApply.newN) {
								Ext.Msg
										.alert("提示",
												"对不起，您已经对考点进行了配置,如果对已安排的考点不满意，请点击'重新安排'按钮，进行重新设置！");
							} else {
								var rows = conductionApply.RPanel
										.getSelectionModel().getSelections();
								if (rows.length < 1) {
									Ext.Msg.show({
												title : '提示',
												msg : '请选择一个或多个选项，进行考场确定！',
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.WARNING
											});
									return false;
								} else {
									conductionApply.manualArrage = true;// 2012-1-20修改
									conductionApply.isRight = true;// 已经确定考点
									var conduction_Ids = []; // 要删除的id
									var personCount = 0;
									var includeCount = 0;
									Ext.each(rows, function(item) {
												conduction_Ids
														.push(item.data.id);
												conductionApply.store
														.removeAll();
												conductionApply.store.add(rows);
												personCount += item.data.personCount;
												includeCount += item.data.includeCount;
											});
									if (conductionApply.personCount > includeCount) {
										Ext.Msg.show({
											title : '错误提示',
											msg : '您选择的考场中报考考生的人数大于考场总的可容考生人数，请修改后进行确认！',
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.ERROR
										});
									} else {
										Ext.Ajax.request({
											url : 'center/confirmConductionPre.action',
											method : 'post',
											params : {
												conduction_Ids : conduction_Ids,
												newN : conductionApply.newN
											},
											success : function(response) {
												var obj = Ext
														.decode(response.responseText);
												if (obj.success == false) {
													Ext.Msg
															.alert("提示",
																	obj.msg);
												} else {
													Ext.Msg
															.alert("提示",
																	obj.msg);
													conductionApply.newN = false;
													Ext
															.getCmp("conductionApply_State")
															.setValue("手动考点分配,考场未分配");
												}
											},
											failure : function(response) {
											}
										});
									}

								}
							}
						}
					}, "-", {
						text : "自动分配考点",
						handler : function() {
							if (conductionApply.manualArrage == true) {
								Ext.Msg.show({
											title : '提示',
											msg : '对不起,您已经手动分配过考点,你不能再自动分配考点！',
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
							} else {
								conductionApply.autoApply = true;// 确定已经自动安排过考点
								if (conductionApply.newN == true) {
									conductionApply.store.proxy
											.setUrl("center/AutoArrangeForConduction.action");
									conductionApply.store.load({
										params : {
											personCount : conductionApply.personCount
										}
									});
									conductionApply.store.proxy
											.setUrl("center/confirmConductionPre.action");
									Ext.getCmp("conductionApply_State")
											.setValue("考点自动分配,考场未分配");
									conductionApply.newN = false;
									Ext.Msg.show({
												title : '提示',
												msg : '考场已经自动安排完毕，请选中考点鼠标右键进行考场安排！',
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.INFO
											});
									positionApply.count = conductionApply.personCount;
								}
							}
						}
					}, '-', {
						text : '自动分配考场',
						handler : function() {
							if (conductionApply.autoApply == false) {
								Ext.Msg.show({
											title : '提示',
											msg : '只有自动安排考点的操作才能进行自动考生安排处理！',
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
							} else {
								Ext.Ajax.request({
									url : 'center/AutoArrangeForPosition.action',
									method : 'post',
									params : {
										startTime : conductionApply.startTime,
										endTime : conductionApply.endTime
									},
									success : function(response) {
										var obj = Ext
												.decode(response.responseText);
										if (obj.success == false) {
											Ext.Msg.alert("提示", obj.msg);
										} else {
											Ext.Msg.alert("提示", obj.msg);
											Ext
													.getCmp("conductionApply_State")
													.setValue("自动分配考点完成,自动分配考场完成");
											conductionApply.autoApplyPosition = true;
											conductionApply.sumPersonCount = 0;
										}
									},
									failure : function(response) {
									}
								});
							}
						}
					}, '-', {
						text : '总完成确认',
						handler : function() {
							if (conductionApply.sumPersonCount != 0) {
								Ext.Msg.show({
											title : '提示',
											msg : '对不起，您还有考生数量没有分配完,请检查分配情况！',
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
							} else {
								Ext.Ajax.request({
									url : 'center/confirmArrageForCP.action',
									method : 'post',
									params : {
										exam_StartTime : conductionApply.startTime,
										exam_Id : conductionApply.exam_Id
									},
									success : function(response) {
										var obj = Ext
												.decode(response.responseText);
										if (obj.success == false) {
											Ext.Msg.alert("提示", obj.msg);
										} else {
											Ext.Msg.alert("提示", obj.msg);
											conductionApply.autoApply = false;
											conductionApply.store.removeAll();
										}
									},
									failure : function(response) {
									}
								});
							}
						}
					}, '-', {
						text : '重置',
						handler : function() {
							conductionApply.store.removeAll();
							conductionApply.store.proxy
									.setUrl("center/showConductionListForCenter.action");
							conductionApply.newN = true;// 重新安排是否已经安排过考点
							conductionApply.autoApply = false;// 重设是否点击自动安排考点
							conductionApply.includeCount = 0;
							Ext.getCmp("conductionApply_includeCount")
									.setValue(conductionApply.includeCount);
							conductionApply.sm.unlock();
							conductionApply.sm
									.deselectRow(conductionApply.selected);
							conductionApply.sm.clearSelections(false);
							Ext.getCmp("conductionApply_State")
									.setValue("没有任何操作");
							Ext.Msg.show({
										title : '提示',
										msg : '请继续双击左面板重新选择考试选项！！',
										buttons : Ext.MessageBox.OK,
										icon : Ext.MessageBox.INFO
									});
						}
					}, '->', "状态栏:", {
						id : 'conductionApply_State',
						xtype : 'displayfield',
						value : "未进行如何操作"
					}, '->', "可容人数:", {
						id : 'conductionApply_includeCount',
						xtype : 'displayfield',
						value : 0
					}, '-', '->', "考生总数:", {
						id : 'conductionApply_personCount',
						xtype : 'displayfield',
						value : 0
					}, '-']
		}
	});
	conductionApply.Main = new Ext.Panel({
				title : '考点安排管理',
				id : 'conductionApplyMainPanel',
				deferredRender : true,
				closeAction : 'hide',
				cm : conductionApply.cm,
				store : conductionApply.store,
				loadMask : true,
				autoWidth : true,
				closable : true,
				layout : 'column',
				viewConfig : {
					forceFit : true
				},
				items : [conductionApply.LPanel, conductionApply.RPanel]
			});
	Ext.getCmp("conductionApply_search_bigType").on('select', function(combo) {
				conductionApply.SmallTypes.load({
							params : {
								big_Id : combo.getValue(),
								limit : 10
							}
						});
			});
	Ext.getCmp("conductionApply_search_smallType").on('select',
			function(combo) {
				conductionApply.lstore.load({
							params : {
								small_Id : combo.getValue(),
								state : 0
							}
						});
			});
	conductionApply.LPanel.on('rowdblclick', function(grid, rowindex, event) {
				var record = grid.getStore().getAt(rowindex);
				conductionApply.l_id=record.get("l_id");
				conductionApply.l_time=record.get("l_time");
				conductionApply.l_etime=record.get("l_etime");
				conductionApply.store.load({
							params : {
								exam_Id : conductionApply.l_id,
								startTime :conductionApply.l_time,
								endTime : conductionApply.l_etime,
								state : 0,
								loadState : 0
							}
						});
				conductionApply.personCount = record.get("l_person_Count");
				conductionApply.newN = true;
				conductionApply.autoApply = false;// 2012年1月20日修改
				conductionApply.autoApplyPosition = false;// 2012年1月20日修改
				conductionApply.manualArrage = false;// 2012年1月20日修改
				conductionApply.sm.unlock();
				conductionApply.sm.deselectRow(conductionApply.selected);
				conductionApply.sm.clearSelections(false);
				Ext.getCmp("conductionApply_personCount")
						.setValue(conductionApply.personCount);
				conductionApply.exam_Id = record.get("l_id");
				conductionApply.startTime = record.get("l_time");
				conductionApply.endTime = record.get("l_etime");
				conductionApply.sumPersonCount = record.get("l_person_Count");
				conductionApply.sumPersonCountR = conductionApply.sumPersonCount;

			});
	conductionApply.menu = new Ext.menu.Menu({
		id : 'conductionApply_Menu',
		items : [{
					id : 'PositionForMenu',
					handler : function() {
						positionApply.Main.show();
						positionApply.lstore.load({
									params : {
										startTime : conductionApply.startTime,
										endTime : conductionApply.endTime,
										unit_Id : conductionApply.menuClick
												.get("id")
									}
								});
					}
				}, {
					text : '状态查询',
					id : 'PositionState',
					handler : function() {
						Ext.Ajax.request({
							url : 'center/confirmConductionState.action',
							method : 'post',
							params : {
								conduction_Id : conductionApply.menuClick
										.get("id")
							},
							success : function(response) {
								var obj = Ext.decode(response.responseText);
								if (obj.success == false) {
									conductionApply.resultForSearch = "没有分配";
									conductionApply.personCountForSearch = 0;
									Ext
											.getCmp("resultForSearch")
											.setValue(conductionApply.resultForSearch);
									Ext
											.getCmp("personCountForSearch")
											.setValue(conductionApply.personCountForSearch);
								} else {
									conductionApply.resultForSearch = "已经分配";
									conductionApply.personCountForSearch = obj.personCount;
									Ext
											.getCmp("resultForSearch")
											.setValue(conductionApply.resultForSearch);
									Ext
											.getCmp("personCountForSearch")
											.setValue(conductionApply.personCountForSearch);
								}
							},
							failure : function(response) {
							}
						});
						conductionApply.showPanel = new Ext.FormPanel({
									defaultType : 'displayfield',
									width : 300,
									labelAlign : 'center',
									constrain : true,
									constrainHeader : true,
									bodyStyle : {
										padding : '30px'
									},
									viewConfig : {
										forceFit : true
									},
									items : [{
										fieldLabel : '考点名称',
										value : conductionApply.menuClick
												.get("name")
									}, {
										fieldLabel : '报考人数',
										value : conductionApply.menuClick
												.get("personCount")
									}, {
										id : 'personCountForSearch',
										fieldLabel : '选择考生人数'
									}, {
										id : 'resultForSearch',
										fieldLabel : '状态'
									}]
								});

						conductionApply.showWindow = new Ext.Window({
									title : '考点状态信息窗口',
									width : 300,
									autoHeight : true,
									modal : true,
									resizable : false,
									animateTarget : 'target',
									items : [conductionApply.showPanel]
								});
						conductionApply.showWindow.show();
					}
				}]
	});
	conductionApply.RPanel.on("rowcontextmenu", function(grid, rowIndex, e) {
		e.preventDefault();
		if (conductionApply.newN == true) {
			Ext.Msg.show({
						title : '提示',
						msg : '请您先对考点进行安排后才能对考场进行安排',
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.INFO
					});
		} else if (conductionApply.autoApplyPosition == true) {
			Ext.Msg.show({
						title : '提示',
						msg : '您已经对考点和考场自动分配，不可以再手动分配，如需重新分配请点击”重新安排“按钮之后重新安排！',
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.ERROR
					});
		} else {
			conductionApply.RPanel.getSelectionModel().selectRow(rowIndex);
			conductionApply.menu.showAt(e.getXY());
			conductionApply.menuClick = conductionApply.RPanel
					.getSelectionModel().getSelected(rowIndex);
			Ext.getCmp("PositionForMenu").setText("'"
					+ conductionApply.menuClick.get("name") + "'考场安排");
			positionApply.conductionId = conductionApply.menuClick.get("id");
			positionApply.personCountLimit = conductionApply.menuClick
					.get("personCount");

		}
	});
	conductionApply.RPanel.getSelectionModel().on('rowselect',
			function(select, rowIndex, record) {
				if (conductionApply.newN == true) {
					conductionApply.includeCount += record.get("includeCount");
					Ext.getCmp("conductionApply_includeCount")
							.setValue(conductionApply.includeCount);
				}
			});
	conductionApply.RPanel.getSelectionModel().on('rowdeselect',
			function(select, rowIndex, record) {
				if (conductionApply.newN == true) {
					conductionApply.includeCount -= record.get("includeCount");
					Ext.getCmp("conductionApply_includeCount")
							.setValue(conductionApply.includeCount);
				}
			});
};
Ext.onReady(conductionApply.init);